package com.qingxin.park.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.qingxin.park.entity.Cropsyz;
import com.qingxin.park.entity.CropsyzType;

import com.qingxin.park.service.CropsyzService;
import com.qingxin.park.service.CropsyzTypeService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;


@Controller
@RequestMapping("/crops_yz")
public class CropsyzAction extends BaseAction {

	@Autowired
	private CropsyzTypeService cropsyzTypeService;
	@Autowired
	private CropsyzService cropsyzService;
	
	
	/*种植大类管理                                                         开始 */
	/**
	 * 种植大类列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdl.do", method=RequestMethod.GET)
	public String listdl(ModelMap model, HttpServletRequest request){
		return "/trace/cropsyz/cropsdl_list";
	}
	
	/**
	 * 种植大类列表分页数据
	 * @param leftdata
	 * @param datatype
	 * @param cropName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listDldata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listDldata(String leftdata, String datatype, String cropName, PageParam pageParam, HttpServletRequest request){
		Pager pager = cropsyzTypeService.list(pageParam, getLoginUser(request));
		return pager;
	}
	
	/**
	 * 添加种植大类信息
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addDl.do" , method=RequestMethod.GET)
	public String addDl(ModelMap model, HttpServletRequest request){
		return "trace/cropsyz/cropsdl_add";
	}
	
	@RequestMapping(value="/cropsyzjstree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object cropsjstree(String pdatatype, String pid){
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<JsTreeNode> list = new ArrayList<>();
			list.add(new JsTreeNode("0", "养殖分类", "ROOT", null, true));
			return list;
			
		}else if("ROOT".equals(pdatatype)){
			String parkid = null;
			List<TreeNode> clist = cropsyzTypeService.getUITreeData(parkid, true);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "TYPE", null, true));
			}
			return list;
		}else if("TYPE".equals(pdatatype)){
			String parkid = null;
			List<TreeNode> clist = cropsyzService.getUITreeData(parkid, pid, false);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "CROP", null, false));
			}
			return list;
		}
		return new ArrayList<Object>();
	}
	
	
	/**
	 * 保存种植大类信息
	 * @param crops
	 * @param result
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/saveDl.json", method={RequestMethod.POST})
	@ResponseBody
	public Object saveDl(@Valid CropsyzType cropsType, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = cropsyzTypeService.add(cropsType,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 修改种植大类信息
	 * @param model
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/editDl.do" , method=RequestMethod.GET)
	public String editDl(ModelMap model, String typeid, HttpServletRequest request){
		CropsyzType cropsyzType = cropsyzTypeService.get(typeid);
		model.put("cropsyzType", cropsyzType);
		return "trace/cropsyz/cropsdl_edit";
	}
	
	
	/**
	 * 保存种植大类信息
	 * @param cropsType
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateDl.json", method={RequestMethod.POST})
	@ResponseBody
	public Object updateDl(@Valid CropsyzType cropsType, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = cropsyzTypeService.update(cropsType, getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除种植大类信息
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/deleteDl.json", method={RequestMethod.POST})
	@ResponseBody
	public Object deleteDl(String[] typeid, HttpServletRequest request){
		if(typeid==null || typeid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return cropsyzTypeService.delete(typeid, getLoginUser(request), request);
	}
	
	
	/*种植大类管理                                                         结束*/
	
	
	
	/**
	 * 农作物列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "/trace/cropsyz/crops_list";
	}
	
	/**
	 * 获取农作物数据
	 * @param parkid
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/getCropData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getCropData(String parkid){
		List<TreeNode> list = cropsyzTypeService.getUITreeData(parkid, true);
		for(TreeNode node : list){
			node.setChildren(cropsyzService.getUITreeData(parkid, node.getId(), false));
		}
		return list;
	}
	
	/**
	 * 加载树形数据
	 * @param pdatatype
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/cropstree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object cropstree(String pdatatype, String pid){
		if(StringUtils.isEmpty(pid)){
			List<TreeNode> list = new ArrayList<TreeNode>();
			list.add(new TreeNode("0", "农作物分类", "ROOT", null, true));
			return list;
			
		}else if("ROOT".equals(pdatatype)){
			String parkid = null;
			return cropsyzTypeService.getUITreeData(parkid, true);
			
		}else if("TYPE".equals(pdatatype)){
			String parkid = null;
			return cropsyzService.getUITreeData(parkid, pid, false);
		}
		return new ArrayList<Object>();
	}
	
	
	/**
	 * 加载树形数据
	 * @param pdatatype
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object lefttree(String pdatatype, String pid){
		if("ROOT".equals(pdatatype)){
			List<TreeNode> rlist = cropsyzTypeService.getUITreeData(null, true);
			//rlist.add(new TreeNode("0", "农作物分类", "ROOT", null, true));
			
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : rlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "CROPS", null,false));
			}
			return list;
			
		}else if("TYPE".equals(pdatatype)){
			List<TreeNode> rlist = cropsyzService.getUITreeData(null, pid, false);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : rlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "CROPS", null,false));
			}
			return list;
		}
		return new ArrayList<Object>();
	}
	
	
	/**
	 * 通用导航菜单数据
	 * @param module
	 * @param lefttab
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/lefttreeA.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	private List<JsTreeNode> lefttreeA(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<TreeNode> tlist = cropsyzTypeService.getUITreeData(null, true);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, true));
			}
		}
		else if("TYPE".equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = cropsyzService.getUITreeData(null, pid, false);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		return list;
	}
	
	/**
	 * 获取树形数据 包含子类
	 * @param parkid
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/lefttreeAll.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object lefttreeAll(){
		List<TreeNode> list = cropsyzTypeService.getUITreeData(null, true);
		
		List<JsTreeNode> jlist = new ArrayList<>();
		for(TreeNode treeNode : list){
			jlist.add(new JsTreeNode(treeNode.getId(), treeNode.getName(),"0", "CROPS", null,true));
			
			List<TreeNode> childlist = cropsyzService.getUITreeData(null, treeNode.getId(), false);
			for(TreeNode node : childlist){
				jlist.add(new JsTreeNode(node.getId(), node.getName(),treeNode.getId(), "CROPS", null,false));
			}
		}
		
		return jlist;
	}
	
	
	/**
	 * 列表分页数据
	 * @param leftdata
	 * @param datatype
	 * @param cropName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String leftdata, String datatype, String cropName, PageParam pageParam, HttpServletRequest request){
		String typeid = null;
		if(!"0".equals(leftdata)){
			typeid = leftdata;
		}
		String parkid = null;
		Pager pager = cropsyzService.list(parkid, typeid, cropName, pageParam, getLoginUser(request));
		return pager;
	}
	
	
	
	/**
	 * 添加农作物信息
	 * @param model
	 * @param leftdata
	 * @param datatype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		if(StringUtils.isEmpty(leftdata)){
			model.put("errormessage", "参数错误");
			return "sys/error/error";
		}
		model.put("typeid", leftdata);
		
		CropsyzType cropsyzType = cropsyzTypeService.get(leftdata);
		model.put("cropsyzType", cropsyzType);
		return "trace/cropsyz/crops_add";
	}
	
	
	/**
	 * 保存农作物信息
	 * @param crops
	 * @param result
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid Cropsyz crops, BindingResult result, String typeid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		String cropMax = cropsyzService.getCropCodeMax();
		if(StringUtils.isNotEmpty(cropMax)){
			int number = Integer.parseInt(cropMax)+1;
			String maxNo = number+"";
			if(maxNo.length()==1){
				maxNo = "00"+maxNo;
			}else if(maxNo.length()==2){
				maxNo = "0"+maxNo;
			}
			crops.setCropCode(maxNo);
		}else{
			crops.setCropCode("001");
		}
		
		SysError info = cropsyzService.add(crops, typeid, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改农作物信息
	 * @param model
	 * @param cropid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String cropid, HttpServletRequest request){
		Cropsyz crops = cropsyzService.get(cropid);
		model.put("cropsyz", crops);
		
		CropsyzType cropsyzType = cropsyzTypeService.get(crops.getCropsyzType().getTypeid());
		model.put("cropsyzType", cropsyzType);
		
		return "trace/cropsyz/crops_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param crops
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid Cropsyz crops, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = cropsyzService.update(crops, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看农作物详情
	 * @param model
	 * @param cropid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String cropid, HttpServletRequest request){
		Cropsyz crops = cropsyzService.get(cropid);
		model.put("cropsyz", crops);
		
		CropsyzType cropsyzType = cropsyzTypeService.get(crops.getCropsyzType().getTypeid());
		model.put("cropsyzType", cropsyzType);
		
		return "trace/cropsyz/crops_view";
	}
	
	
	
	/**
	 * 删除农作物信息
	 * @param cropid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] cropid, HttpServletRequest request){
		if(cropid==null || cropid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return cropsyzService.delete(cropid, getLoginUser(request), request);
	}
	
	
	
}
